<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"/>
    <title>登录 - zealously后台管理系统</title>
    <link rel="icon" th:href="@{/admin/images/favicon.ico}" type="image/ico">
    <meta name="keywords" content="blog"/>
    <meta name="description" content="blog"/>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css">
    <link th:href="@{/admin/css/materialdesignicons.min.css}" rel="stylesheet">
    <link th:href="@{/admin/css/style.min.css}" rel="stylesheet">
    <style>
        .lyear-wrapper {
            position: relative;
        }

        .lyear-login {
            display: flex !important;
            min-height: 100vh;
            align-items: center !important;
            justify-content: center !important;
        }

        .lyear-login:after {
            content: '';
            min-height: inherit;
            font-size: 0;
        }

        .login-center {
            background: #ffffff;
            min-width: 29.25rem;
            padding: 2.14286em 3.57143em;
            border-radius: 5px;
            margin: 2.85714em;
        }

        .login-header {
            margin-bottom: 1.5rem !important;
        }

        .login-center .has-feedback.feedback-left .form-control {
            padding-left: 38px;
            padding-right: 12px;
        }

        .login-center .has-feedback.feedback-left .form-control-feedback {
            left: 0;
            right: auto;
            width: 38px;
            height: 38px;
            line-height: 38px;
            z-index: 4;
            color: #dcdcdc;
        }

        .login-center .has-feedback.feedback-left.row .form-control-feedback {
            left: 15px;
        }

        #error {
            animation: color_change 10000ms ease-out infinite alternate;
            font-size: 15px;
            color: #f96868;
        }
    </style>
</head>

<body background="/admin/images/login-bg.png">
<div class="row lyear-wrapper">
    <div class="lyear-login">
        <div class="login-center">
            <div class="login-header text-center">
                <a href="index.html"> <img alt="light year admin" th:src="@{/admin/images/logo-sidebar.png}"> </a>
            </div>
            <form id="login" th:method="post">
                <div class="form-group has-feedback feedback-left">
                    <input type="text" placeholder="请输入您的用户名" class="form-control" name="username" id="username"/>
                    <span class="mdi mdi-account form-control-feedback" aria-hidden="true"></span>
                </div>
                <div class="form-group has-feedback feedback-left">
                    <input type="password" placeholder="请输入密码" class="form-control" id="password" name="password"/>
                    <span class="mdi mdi-lock form-control-feedback" aria-hidden="true"></span>
                </div>
                <div class="form-group has-feedback feedback-left row">
                    <div class="col-xs-7">
                        <input type="text" name="captcha" class="form-control" placeholder="验证码">
                        <span class="mdi mdi-check-all form-control-feedback" aria-hidden="true"></span>
                    </div>

                    <div class="col-xs-5">
                        <img th:src="@{/captcha}" class="pull-right" id="captcha"
                             style="cursor: pointer;" onclick="verifyCode();" title="点击刷新"
                             alt="captcha">
                        <input type="hidden" id="cKey" name="cKey">
                    </div>
                </div>
                <div class="col-sm-12 text-center">
                    <p id="error"></p>
                </div>
                <div class="form-group">
                    <button class="btn btn-block btn-primary" onclick="login()" type="button">立即登录</button>
                </div>
            </form>
            <hr>
            <footer class="col-sm-12 text-center">
                <p class="m-b-0">Copyright © 2021 <a href="http://lyear.itshubao.com">荔枝博客</a>. All right reserved</p>
            </footer>
        </div>
    </div>
</div>
<script src="https://cdn.jsdelivr.net/gh/jquery/jquery@3.2.1/dist/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js"></script>
<script type="text/javascript">;</script>
<script>
    $(function () {
        verifyCode();
    });

    // 获取验证码
    function verifyCode() {
        $.get('/captcha', function (res) {
            $('#cKey').attr('value', res.key);
            $('#captcha').attr('src', res.image);
        }, 'json');
    }

    function login() {
        $.ajax({
            type: "POST",
            dataType: "json",
            url: "/auth/login",
            async: false,
            data: JSON.stringify({
                username: $("#username").val(),
                password: $("#password").val(),
                captcha: $("[name ='captcha']").val(),
                cKey: $("#cKey").val(),
            }),
            success: function (result) {
                if (result.code === 200) {
                    location.href = "/admin/index";
                } else {
                    $('#error').html(result.error);
                }
            },
        });
    }
</script>
</body>
</html>